﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    public partial class WenDataGridViewFilterControl : WenSkin.Forms.WenForm
    {
        private readonly WenDataGridView WenDataGridView;
        private readonly DataGridViewColumn DataGridViewColumn;

        public WenDataGridViewFilterControl(WenDataGridView dataGridView,DataGridViewColumn dataGridViewColumn)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            this.Location = MousePosition;
            this.ShowInTaskbar = false;
            this.TopMost = true;
            this.WenDataGridView = dataGridView;
            this.DataGridViewColumn = dataGridViewColumn;
            this.Stretching = false;
            this.Text = $"筛选 {dataGridViewColumn.HeaderText}";
            ReListData();
        }

        #region 委托

        public event WenDataGridViewFilterEventHandler WenDataGridViewFilter;

        public delegate void WenDataGridViewFilterEventHandler(object sender, WenDataGridViewFilterEventArgs e);
        public class WenDataGridViewFilterEventArgs: EventArgs
        {
            public WenDataGridViewFilterEventArgs(DataGridView dataGridView, DataGridViewColumn dataGridViewColumn, List<DataGridViewRow> showRows, List<DataGridViewRow> hideRows)
            {
                DataGridView = dataGridView;
                DataGridViewColumn = dataGridViewColumn;
                ShowRows = showRows;
                HideRows = hideRows;
            }

            public DataGridView DataGridView { get; private set; }

            public DataGridViewColumn DataGridViewColumn { get; private set; }

            public List<DataGridViewRow> ShowRows { get; private set; }

            public List<DataGridViewRow> HideRows { get; private set; }
        }

        private void EWenDataGridViewFilter()
        {
            List<DataGridViewRow> showRows = new List<DataGridViewRow>();
            List<DataGridViewRow> hideRows = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in WenDataGridView.Rows)
            {
                if (row.Visible)
                    showRows.Add(row);
                else
                    hideRows.Add(row);
            }
            var e = new WenDataGridViewFilterEventArgs(this.WenDataGridView, this.DataGridViewColumn, showRows, hideRows);
            OnWenDataGridViewFilter(e);
        }

        protected virtual void OnWenDataGridViewFilter(WenDataGridViewFilterEventArgs e)
        {
            WenDataGridViewFilter?.Invoke(this, e);
        }

        #endregion

        #region 排序
        //升序
        private void wenButton1_Click(object sender, EventArgs e)
        {
            this.WenDataGridView.CurrentCell = null;
            WenDataGridView.Sort(DataGridViewColumn, ListSortDirection.Ascending);
        }

        //降序
        private void wenButton2_Click(object sender, EventArgs e)
        {
            this.WenDataGridView.CurrentCell = null;
            WenDataGridView.Sort(DataGridViewColumn, ListSortDirection.Descending);
        }
        #endregion

        #region 文本以及列表处理
        private List<string> DataFilterList = new List<string>();
        //列出出全部
        private void ReListData()
        {
            foreach (DataGridViewRow row in WenDataGridView.Rows)
            {
                //累积筛选已经筛选选项直接隐藏
                if (row.Visible == false)
                {
                    continue;
                }

                string text = row.Cells[DataGridViewColumn.Name]?.Value?.ToString();

                if (!DataFilterList.Exists(q => q == text))
                {
                    DataFilterList.Add(text);
                }
            }

            if (WenDataGridView.FilterColumns.Count > 0 && WenDataGridView.FilterColumns.Last().Key == this.DataGridViewColumn)
                foreach (var item in WenDataGridView.FilterColumns[this.DataGridViewColumn])
                {
                    DataFilterList.Add(item);
                }

            DataFilterList.Sort();
            ReCheckedListBoxItem();

            //第一次将满足条件的false
            if (WenDataGridView.FilterColumns.ContainsKey(this.DataGridViewColumn))
            {
                List<string> hideString = WenDataGridView.FilterColumns[this.DataGridViewColumn];
                for (int i = 0; i < wenCheckedListBox1.Items.Count; i++)
                {
                    if (hideString.Contains(wenCheckedListBox1.Items[i]?.ToString()))
                    {
                        wenCheckedListBox1.SetItemChecked(i, false);
                    }
                }
            }
        }

        //筛选数据
        private void ReCheckedListBoxItem(string text="")
        {
            wenCheckedListBox1.Items.Clear();
            foreach (var item in DataFilterList)
            {
                if (item == null)
                    continue;
                if(item.ToUpper().Contains(text.ToUpper()))
                {
                    wenCheckedListBox1.Items.Add(item, true);
                }
                else
                {
                    wenCheckedListBox1.Items.Add(item, false);
                }
            }
        }

        private void wenTextBox1_TextChanged(object sender, EventArgs e)
        {
            ReCheckedListBoxItem(wenTextBox1.Text);
        }
        #endregion

        #region 筛选
        //开始筛选
        private void wenButton4_Click(object sender, EventArgs e)
        {
            this.WenDataGridView.CurrentCell = null;
            List<string> hide = new List<string>();
            List<string> show = new List<string>();
            for (int i = 0; i < wenCheckedListBox1.Items.Count; i++)
            {
                if (wenCheckedListBox1.GetItemChecked(i))
                {
                    show.Add(wenCheckedListBox1.Items[i]?.ToString());
                }
                else
                {
                    hide.Add(wenCheckedListBox1.Items[i]?.ToString());
                }
            }
            if (this.WenDataGridView.FilterColumns.ContainsKey(this.DataGridViewColumn))
            {
                this.WenDataGridView.FilterColumns[this.DataGridViewColumn].RemoveAll(q => show.Contains(q));
                this.WenDataGridView.FilterColumns[this.DataGridViewColumn].RemoveAll(q => hide.Contains(q));
                this.WenDataGridView.FilterColumns[this.DataGridViewColumn].AddRange(hide);
            }
            else
                this.WenDataGridView.FilterColumns.Add(this.DataGridViewColumn, hide);
            WenDataGridView.FilterWenDataGridViewRow();
            EWenDataGridViewFilter();
            this.Close();
        }
        #endregion

        #region 选择
        //全选
        private void wenButton5_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < wenCheckedListBox1.Items.Count; i++)
            {
                wenCheckedListBox1.SetItemChecked(i, true);
            }
        }
        //取消全选
        private void wenButton6_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < wenCheckedListBox1.Items.Count; i++)
            {
                wenCheckedListBox1.SetItemChecked(i, false);
            }
        }
        #endregion

        private void wenButton3_Click(object sender, EventArgs e)
        {
            this.WenDataGridView.FilterColumns.Remove(this.DataGridViewColumn);
            WenDataGridView.FilterWenDataGridViewRow();
            EWenDataGridViewFilter();
            this.Close();
        }
    }
}
